package vip.xiaonuo.common.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @author zjt
 * @description
 * @date 2025/3/11 09:03
 */
@Slf4j
public class CommonHttpUtil {

    public static String get(String url, String type) {
        long apiBeginTime = System.currentTimeMillis();
        HttpRequest post = HttpUtil.createGet(url);
        // 设定请求头
        post.header(Header.CONTENT_TYPE, "application/json");
        //设置超时时间为5秒
        post.timeout(5000);
        HttpResponse execute = post.execute();
        String respBody = execute.body();
        log.debug("{}接口请求Url:{}", type, url);
        log.debug("{}接口响应参数:{}", type, respBody);
        log.debug("{}本次调用远端接口耗时:{}", type, System.currentTimeMillis() - apiBeginTime);
        int statusCode = execute.getStatus();
        if (statusCode == 200) {
            return respBody;
        } else {
            log.error("{}-->接口调用出现异常{}，调用失败，请求报文为：{}", type, statusCode, post);
            return null;
        }
    }

    public static String post(String url, Map<String, Object> headerMap, String bodyStr, String type) {
        return postWithTimeOut(url, headerMap, bodyStr, type, 5000);
    }

    public static String postWithTimeOut(String url, Map<String, Object> headerMap, String bodyStr, String type, int timeout) {
        long apiBeginTime = System.currentTimeMillis();
        HttpRequest post = HttpUtil.createPost(url);
        // 设定请求头
        post.header(Header.CONTENT_TYPE, "application/json");
        if (CollectionUtil.isNotEmpty(headerMap)) {
            for (Map.Entry<String, Object> entry : headerMap.entrySet()) {
                post.header(entry.getKey(), entry.getValue().toString());
            }
        }
        // 设定请求体
        log.debug("{}-->接口请求Url:{},接口请求body:{}", type, url, bodyStr);
        post.body(bodyStr);
        //设置超时时间为5秒
        post.timeout(timeout);
        HttpResponse execute = post.execute();
        String respBody = execute.body();
        log.debug("接口响应参数:{}", respBody);
        log.debug("本次调用远端接口耗时:{}", System.currentTimeMillis() - apiBeginTime);
        int statusCode = execute.getStatus();
        if (statusCode == 200) {
            return respBody;
        } else {
            log.error("{}-->接口调用出现异常{}，调用失败，我方请求报文为：{}", type, statusCode, post);
            return null;
        }
    }

    public static String postByForm(String url, Map<String, Object> formMap, String type) {
        long apiBeginTime = System.currentTimeMillis();
        HttpRequest post = HttpUtil.createPost(url);
        // 设定请求头
        post.header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded");
        if (CollectionUtil.isNotEmpty(formMap)) {
            for (Map.Entry<String, Object> entry : formMap.entrySet()) {
                post.form(entry.getKey(), String.valueOf(entry.getValue()));
            }
            // 设置超时时间为5秒
            post.timeout(5000);
        }
        // 记录请求信息
        log.debug("{}-->接口请求Url:{}，请求头:{}", type, url, post);
        HttpResponse execute = post.execute();
        String respBody = execute.body();
        log.debug("接口响应参数:{}", respBody);
        log.debug("本次调用远端接口耗时:{}", System.currentTimeMillis() - apiBeginTime);
        int statusCode = execute.getStatus();
        if (statusCode == 200) {
            return respBody;
        } else {
            log.error("{}-->接口调用出现异常{}，调用失败", type, statusCode);
            return null;
        }
    }
}
